package com.wzc.order.controller;

import com.wzc.basic.util.AjaxResult;
import com.wzc.basic.util.LoginContext;
import com.wzc.basic.util.PageList;
import com.wzc.order.domain.OrderPetAcquisition;
import com.wzc.order.dto.OrderDto;
import com.wzc.order.query.OrderPetAcquisitionQuery;
import com.wzc.order.service.IOrderPetAcquisitionService;
import com.wzc.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController   // @Controller 和
@RequestMapping("/orderPetAcquisition")
@Api(value = "订单接口类",description = "订单相关CRUD")
public class OrderPetAcpuistionController {


    @Autowired
    private IOrderPetAcquisitionService orderPetAcquisitionService;

    /**
     * 查询所有
     * @GetMapping
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<OrderPetAcquisition> loadAll(HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return orderPetAcquisitionService.loadAll();
    }


    /**
     * 查询单个
     * @GetMapping("/{id}") 后端接收前端传输的数据（动态传输）
     * @PathVariable("id") 从路径上去获取id变量（后端取数据）再赋值给 Long id
     * @param id
     * @return
     */
    @GetMapping("/{id}")  // 接收参数 @PathVariable获取参数
    @ApiOperation(value = "通过id查询单个数据")
//    @PreAuthorize(name = "订单管理通过id查询单个数据", sn = "orderPetAcquisition:loadById")
    public OrderPetAcquisition loadById(@PathVariable("id") Long id){
        return orderPetAcquisitionService.loadById(id);
    }

    /**
     * @RequestBody  从请求体中获取数据赋值给orderPetAcquisitionQuery
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            return orderPetAcquisitionService.queryQuery(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<OrderPetAcquisition>();
        }
    }


//    /**
//     * 删除
//     * @DeleteMapping("/{id}") 后端接收前端参数
//     * @PathVariable("id")  从资源路径上获取参数id，在赋值给Long id
//     * @param id
//     * @return
//     */
//    @DeleteMapping("/{id}")
//    @ApiOperation(value = "删除")
////    @PreAuthorize(name = "订单管理删除操作", sn = "orderPetAcquisition:delete")
//    public AjaxResult delete(@PathVariable("id") Long id){
//        try {
//            orderPetAcquisitionService.remove(id);
//            return AjaxResult.me();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return AjaxResult.me().setMsg("删除失败");
//        }
//    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
//    @PreAuthorize(name = "订单管理批量删除操作", sn = "orderPetAcquisition:patchDelete")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            orderPetAcquisitionService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setMsg("操作失败");
        }

    }




    /**
     * 添加和修改
     * @PutMapping 请求体携带参数
     * @RequestBody  从请求体中获取参数
     * @param orderPetAcquisition
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改")
//    @PreAuthorize(name = "订单管理添加或修改", sn = "orderPetAcquisition:saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition){
        try {
            if (orderPetAcquisition.getId() != null){
                orderPetAcquisitionService.update(orderPetAcquisition);
            } else {
                orderPetAcquisitionService.save(orderPetAcquisition);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败");
        }
    }


    /**
     * 订单确认
     * @param
     * @param
     * @return
     */
    @PostMapping("/confirm")
    @ApiOperation(value = "订单确认")
    public AjaxResult confirm(@RequestBody OrderDto dto){
        try {
            orderPetAcquisitionService.confirm(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("订单确认失败！");
        }
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "取消订单")
    public AjaxResult cancel(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.cancel(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("订单取消失败！！！");
        }
    }

}
